package com.quanyan.club.task.tracker;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.club.entity.db.TbClub;
import com.quanyan.club.entity.db.TbClubActivity;
import com.quanyan.club.entity.db.TbClubMember;
import com.quanyan.club.entity.db.TbClubMemberExample;
import com.quanyan.club.mapper.TbClubActivityMapper;
import com.quanyan.club.mapper.TbClubMapper;
import com.quanyan.club.mapper.TbClubMemberMapper;
import com.quanyan.club.service.CppService;
import com.quanyan.common.constants.Constants;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by zhuo.shi on 2016/9/21.
 *
 * AA 活动结束，第3、7、15天各自发一个通知结算
 */
@Component
public class Activity4AAEndNotifyJob implements JobRunner {

    private static Logger logger = LoggerFactory.getLogger(Activity4AAEndNotifyJob.class);

    @Autowired
    private CppService cppService;
    @Autowired
    private TbClubMapper clubMapper;
    @Autowired
    private TbClubMemberMapper clubMemberMapper;
    @Autowired
    private TbClubActivityMapper tbClubActivityMapper;

    @Override
    public Result run(JobContext jobContext) throws Throwable {

        Job job = jobContext.getJob();
        if (job == null) {
            return new Result(Action.EXECUTE_EXCEPTION, "任务不存在");
        }

        Map params = job.getExtParams();

        //1. 获取活动信息，俱乐部信息
        int clubId = MapUtils.getIntValue(params, "clubId");
        int activityId = MapUtils.getIntValue(params, "activityId");
        int clubActivityId = MapUtils.getIntValue(params, "clubActivityId");
        String activityName = MapUtils.getString(params, "activityName");
        int timelong = MapUtils.getIntValue(params, "timelong");  //第3、7、15天任务
        logger.warn("AA结算提醒任务：clubId={}, activityId={}", clubId, activityId);

        TbClubActivity tbClubActivity = tbClubActivityMapper.selectByPrimaryKey(clubActivityId);
        if (tbClubActivity == null){
            return new Result(Action.EXECUTE_FAILED);
        }
        if (tbClubActivity.getIsSettle() == 1){
            logger.warn("活动已经结算，不进行AA结算任务提醒，返回");
            return new Result(Action.EXECUTE_SUCCESS);
        }

        TbClub tbClub = clubMapper.selectByPrimaryKey(clubId);
        if (tbClub == null){
            logger.error("AA结算提醒任务，俱乐部{}不存在！" , clubId);
            return new Result(Action.EXECUTE_FAILED);
        }

        String message = tbClub.getClubName() +"俱乐部的管理员，您还有" + activityName + "活动未结算，请及时进行结算, ";
        if (timelong == 3 || timelong == 7) {
            message += "15天内若还未结算，我们将自动为您结算。";
        }
        if (timelong == 15){
            message += "今天是结算最后一天，如果还未结算我们将自动为您结算。";
        }

        TbClubMemberExample tbClubMemberExample = new TbClubMemberExample();
        tbClubMemberExample.createCriteria()
                .andClubIdEqualTo(clubId)
                .andRoleCodeIn(Arrays.asList(
                        Constants.ClubRole.CLUB_ROLE_PRESIDENT,
                        Constants.ClubRole.ROLE_CLUB_ADMIN,
                        Constants.ClubRole.ROLE_CLUB_FINANCIAL))
                .andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        List<TbClubMember> resultList = clubMemberMapper.selectByExample(tbClubMemberExample);
        List<Integer> adminUids = new ArrayList<>();
        for (TbClubMember member : resultList){
            adminUids.add(member.getUid());
        }

        return new Result(Action.EXECUTE_SUCCESS);
    }
}
